//
//  SubscritoViewController.m
//  misDeseos
//
//  Created by Roger Plana Casadella on 12/11/11.
//  Copyright (c) 2011 Champions Freak. All rights reserved.
//

#import "SubscritoViewController.h"
#import "MainViewController.h"

@implementation SubscritoViewController

@synthesize taula;
@synthesize mainView;
@synthesize infoListas;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
    }
    return self;
}

#pragma mark - ASIHTTPRequest Delegate Methods

-(void)processResponse:(NSString *)response
{
    //Treiem el monitor d'activitat
    [DSBezelActivityView removeViewAnimated:NO];
    
    
    NSMutableDictionary *loginData = [response JSONValue];
    NSString *responseCode = [loginData objectForKey:@"type"];
    if ([responseCode isEqualToString:@"response"])
    {
        NSString *stat = [loginData objectForKey:@"stat"];
        if ([stat isEqualToString:@"ok"])
        {
            /*
             NSMutableDictionary *respData = [loginData objectForKey:@"data"];
             [mvc setUserName:[respData objectForKey:@"name"]];
             [mvc setHashID:[respData objectForKey:@"hash"]];
             */
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"El nombre de usuario i password no son correctos"
                                                           delegate:self 
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            
            [alert show];
            [alert release];
        }
    }
    else
    {
        NSString *errorStr = [loginData objectForKey:@"msn"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:errorStr
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    [self processResponse:[request responseString]];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    [DSBezelActivityView removeViewAnimated:YES];
    if ([error code] == 2)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:@"The request timed out, try again later"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:@"There has been an error with the server, pleas try again later"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release]; 
    }
    NSLog(@"ERROR --> %@",[error localizedDescription]);
}

#pragma mark - Table View Data Sourece Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.infoListas count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    if (self.selectedList == nil)
    {
        static NSString *CellIdentifier = @"MisListasCellIdentifier";
        MisListasTableViewCell *cell = (MisListasTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) 
        {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"MisListasTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        
        // Set up the cell...
        NSArray *arr = [self.infoListas objectAtIndex:indexPath.row];
        cell.title.text = [arr objectAtIndex:MIS_LISTAS_LIST_TITLE];
        cell.timeLeft.text = [NSString stringWithFormat:@"Finaliza: %@",[arr objectAtIndex:MIS_LISTAT_LIST_END_DATE]];
        
        switch ([[arr objectAtIndex:MIS_LISTAT_LIST_CAT] intValue]) {
            case 1:
                cell.icon.image = [UIImage imageNamed:@"icon_boda.png"];
                break;
                
            case 4:
                cell.icon.image = [UIImage imageNamed:@"icon_cumple.png"];
                break;
        }
        
        return cell;
    }
    else
    {
        static NSString *CellIdentifier = @"MisListasRegalosCellIdentifier";
        MisListasRegalosTableViewCell *cell = (MisListasRegalosTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) 
        {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"MisListasRegalosTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        
        // Set up the cell...
        NSArray *arr = [self.listItems objectAtIndex:indexPath.row];
        cell.title.text = [arr objectAtIndex:1];
        cell.encuentralo.text = [arr objectAtIndex:2];
        
        switch ([[arr objectAtIndex:3] intValue]) {
            case 1:
                cell.icon.image = [UIImage imageNamed:@"icon_videojuegos.png"];
                break;
                
            case 2:
                cell.icon.image = [UIImage imageNamed:@"icon_electro.png"];
                break;
        }
        return cell;
    }
    */
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    if (self.selectedList == nil)
    {
        self.selectedList = indexPath;
        NSArray *arr = [self.infoListas objectAtIndex:indexPath.row];
        titleRegalos.text = [arr objectAtIndex:MIS_LISTAS_LIST_TITLE];
        
        //posicionem la vista que volem veure al derrera
        CGRect vistaEntrant = [regalosView frame];
        vistaEntrant.origin.x = 360;
        [regalosView setFrame:vistaEntrant];
        
        CGRect vistaSortint = [mainView frame];
        vistaEntrant.origin.x = 0;
        vistaSortint.origin.x = -360;
        
        [UIView animateWithDuration:0.5
                              delay:0.0
                            options: UIViewAnimationCurveEaseInOut
                         animations:^{
                             [regalosView setFrame:vistaEntrant];
                             [mainView setFrame:vistaSortint];
                         } 
                         completion:^(BOOL finished){
                             [self regalsRequest];
                         }];
    }
    */
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [super dealloc];
}

@end
